const process = require('child_process');
const {parse} = require("node-html-parser");
const path = require('path');
const fs = require('fs')
const moment = require('moment')

const fileIsNewly = (fileapth)=>{
    if(fs.existsSync(fileapth)){
        const time = fs.statSync(fileapth).mtime || fs.statSync(fileapth).ctime
        const datetimeNow = moment().unix()
        const timeFrom = moment(time).unix()
        const delta = datetimeNow - timeFrom
        return delta < 60 * 60
    }
    return false
}

const getProxies = async (zipcode,keyword)=>{

    return new Promise(async (resolve,reject)=>{

        const jsonPath = path.resolve(__dirname,'config',"proxies.json")
        if(fileIsNewly(jsonPath)){
            const content = fs.readFileSync(jsonPath);
            const proxies = JSON.parse(content)
            resolve(proxies)
            return
        }
        const start = async (page=0)=>{
            let result = []

            const htmlPath = path.resolve(__dirname,'config',`proxies_${page}.html`)
            const download = ()=>{
                let url = 'https://www.freeproxy.world/?type=http&anonymity=&country=&speed=1000&port=80';
                if(page>0){
                    url += `&page=${page}`
                }
                process.execSync(`curl '${url}' -o '${htmlPath}'`)   
            }
            if(!fileIsNewly(htmlPath)){
                download()
            }else{
                download()
            }
            const htmlBuffer = fs.readFileSync(htmlPath);
            const html = htmlBuffer.toString('utf-8')
            const htmlEle = parse(html)
            const ip_ports = htmlEle.querySelectorAll("table.layui-table tbody tr").map((e)=>{
                const ipele = e.querySelector('td.show-ip-div');
                const portele = e.querySelector('td a[href]')
                var ip = ""
                var port = ""
                if(ipele && portele){
                     ip = ipele.innerText.replace(/\s/g,"")
                     port = portele.innerText
                }
                return {ip,port}
            }).filter((v)=> v.ip !== '')


          let tasks = ip_ports.map((proxy,i)=>{
            return new Promise((rs,rj)=>{
                process.exec(`ping -c 1 ${proxy.ip}`,(err,stdout,stderr)=>{
                    if(!err){
                        result.push(proxy)
                        rs(true)
                    }else{
                        rj(err)
                        console.log(`got err while try connect to ${proxy.ip}`)
                    }
                })    
            })
           })
           await Promise.allSettled(tasks);
            return result;
         }

        const r0 = await start()||[];
        const r1 = await start(1)||[]
        const r = r0.concat(r1)
        fs.writeFileSync(jsonPath,JSON.stringify(r));
        resolve(r)
     }) 
} 

module.exports = {
    getProxies
};